package my.download;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import wxg.utils.ContentTypeAndExtension;
import wxg.utils.My;

/**
 * 
 * 
 * 
 * 1) https://mvnrepository.com/artifact/com.google.api-client/google-api-client
 * -	* google-api
 * 2) https://mvnrepository.com/artifact/org.jsoup/jsoup
 * -	* jsoup
 * 3) https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
 * -	* okhttp
 * 4) https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
 * -	* httpclient
 * -	* http://hc.apache.org/httpclient-3.x/
 * -	* http://hc.apache.org/httpcomponents-client-ga/index.html
 * 
 * @see org.apache.http.client.HttpClient
 * @see org.apache.http.impl.client.HttpClients
 * 
 * @author Wxg
 *
 */
public class DownloadImagesDemo2 {

	private static int id = 1;
	
	/**
	 * 
	 * 
	 * 1) read会阻塞的,所以不用担心,内部细节; 
	 * 2) 只用关系read的返回值"readSize",这个值为"-1"时表明已经读取流的末尾处, 这时应该退出循环; 
	 * 否则就表明向buffer中填充了"readSize"长度的数据; 可能填满,也可能未填满
	 * 
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		final String sp = File.separator;
		final String dir = My.prop("mainconf.properties").getProperty("DownloadStore");
		
		CloseableHttpClient client = HttpClients.createDefault();
		final ContentTypeAndExtension cte = new ContentTypeAndExtension();
		
		
		String url = "http://img.hb.aicdn.com/778466793fd1fd8801ecb60e85801296c98b3844459bd-G41ZnT_fw658";
		HttpGet get = new HttpGet(url);
		try {
			client.execute(get, new ResponseHandler<String>(){
				@Override
				public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
//					Header[] headers = response.getAllHeaders();
//					for(Header h : headers){
//						System.out.println( h );
//					}
					
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						String contentType = entity.getContentType().getValue();
						String ext = cte.getSingleExtensions(contentType);
						System.out.printf("ext : %s", ext);
						InputStream is = entity.getContent();
						BufferedInputStream bis = new BufferedInputStream(is);
						File file = new File(dir + sp + My.ss(id++) + ext);
						BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
						byte[] buffer = new byte[1024];
						int len = 0;
						try {
							while( (len = bis.read(buffer)) != -1 ){
								bos.write(buffer, 0, len);
							}
						} finally {
							bos.close();
							// bis.close(); // client.close(); >> bis will close?!
						}
						
					}
					return null;
				}
			});
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
